package logic

import (
	"box-mart-api/boxrpc/boxrpc"
	"box-mart-api/boxrpc/internal/svc"
	"box-mart-api/models/mysql"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type BoxAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBoxAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BoxAddLogic {
	return &BoxAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *BoxAddLogic) BoxAdd(in *boxrpc.BoxAddRequest) (*boxrpc.BoxAddResponse, error) {
	// todo: add your logic here and delete this line
	var box mysql.Box
	var err error
	//err := box.FindByBox(mysql.DB, in.BoxId)
	//if err != nil {
	//	return nil, errors.New("商品存在,不可重复")
	//}

	box = mysql.Box{
		Model:    gorm.Model{},
		BoxTitle: in.BoxTitle,
		BoxName:  in.BoxName,
		BoxPrice: in.BoxPrice,
		BoxType:  in.BoxType,
	}
	err = box.BoxAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &boxrpc.BoxAddResponse{
		BoxId: int64(uint(int64(box.ID))),
	}, nil
}
